function title() {
	var div = document.createElement("div");//新建标签元素
    var body = document.body;
    div.innerHTML = '<div id="extend_login_title">登录后将自动绑定</div>';
    div.style = 'width: 100%; background-color: red; text-align: center; line-height: 50px; color: #fff; position: fixed; left: 0; top: 0; z-index: 999999;';
    body.parentNode.insertBefore(div,body);
}

function login_succ(flag) {
    var div = document.createElement('div');
    div.innerText = flag ? '登录成功, 5秒后关闭当前页面' : '当前登录账号与待授权账号不符, 请重新登录';
    div.style = 'position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); padding: 16px 10px; color: white; width: 300px; line-height: 21px; text-align: center; font-size: 18px; background-color: ' + (flag ? '#43b244' : 'red') + '; z-index: 99999;';
    document.body.appendChild(div);
    setTimeout(() => {
        div.remove();
        send_Background({code: 'closeTab', url: window.location.href});
    }, 5000);
}


async function getElement(type, key, index=0, retry=1, wait=0) {
	var dom = null;
	__checkParames('getElement', type, key, index, retry, wait);
	switch (type) {
		case 'id':
			for (var i = 0; i < retry; i++) {
				dom = document.getElementById(key);
				await delay(wait);
				if (dom) break;
			}
			break;
		case 'class':
		case 'tag':
		case 'name':
			var doms = await getElements(type, key, retry, wait);
			dom = doms ? doms [index] : null;
			break;
		default:
			throw new TypeError('type can only be "id" or "class" or "tag" or "name"!');
			break;
	}
	return dom;
}

async function getElements(type, key, retry=1, wait=0) {
	var doms = null;
	__checkParames('getElements', type, key, null, retry, wait);
	for (var i = 0; i < retry; i++) {
		switch (type) {
			case 'class':
				doms = document.getElementsByClassName(key);
	 			break;
			case 'tag':
				doms = document.getElementsByTagName(key);
				break;
			case 'name':
				doms = document.getElementsByName(key);
				break; 
			default:
				throw new TypeError('type can only be "class" or "tag" or "name"!');
				break;
		}
		await delay(wait);
		if (doms) break;
	}
	return doms;
}

async function getElement4Iframe(iframe, type, key, index=0, retry=1, wait=0) {
	var dom = null;
	__checkParames('getElement', type, key, index, retry, wait);
	try {
		switch (type) {
			case 'id':
				for (var i = 0; i < retry; i++) {
					dom = iframe.contentWindow.document.getElementById(key);
					await delay(wait);
					if (dom) break;
				}
				break;
			case 'class':
			case 'tag':
			case 'name':
				var doms = await getElements4Iframe(iframe, type, key, retry, wait);
				dom = doms ? doms [index] : null;
				break;
			default:
				throw new TypeError('type can only be "id" or "class" or "tag" or "name"!');
				break;
		}
	} catch(e) {}
	return dom;
}

async function getElements4Iframe(iframe, type, key, retry=1, wait=0) {
	var doms = null;
	__checkParames('getElements', type, key, null, retry, wait);
	try {
		for (var i = 0; i < retry; i++) {
			switch (type) {
				case 'class':
					doms = iframe.contentWindow.document.getElementsByClassName(key);
		 			break;
				case 'tag':
					doms = iframe.contentWindow.document.getElementsByTagName(key);
					break;
				case 'name':
					doms = iframe.contentWindow.document.getElementsByName(key);
					break; 
				default:
					throw new TypeError('type can only be "class" or "tag" or "name"!');
					break;
			}
			await delay(wait);
			if (doms) break;
		}
	} catch(e) {}
	return doms;
}

function __checkParames(fun, type, key, index, retry, wait) {
	if ('string' != typeof(type) || '' == type) throw new TypeError('type must be String and can`t be empty!');
	if ('string' != typeof(key) || '' == key) throw new TypeError('key must be String and can`t be empty!');
	if ('getElement' == fun && ('number' != typeof(index) || 0 > index)) throw new TypeError('index must be Integer and must be greater than 0!');
	if ('number' != typeof(retry) || 0 >= retry) throw new TypeError('retry must be Integer and must be greater than 0!');
	if ('number' != typeof(wait) || 0 > wait) throw new TypeError('wait must be Integer and must be greater than 0!');
}


function send_Background(message) {
	return new Promise(resolve => {
		chrome.runtime.sendMessage(message, function(response) {resolve(response.result)});
	});
}

/**
* 判断字符串是否为空
* @parame obj 字符串
*/
function check_StrIsNull(obj) {
	return null == obj || obj.replace(/(^s*)|(s*$)/g, "").length == 0;
}


/**
 * 延时
 * @param  {[type]} delay [description]
 * @return {[type]}       [description]
 */
function delay(delay) {
    return new Promise(resolve => {
        setTimeout(() => resolve("delay"), 1000 * delay);
    });
}